/*
 * Copyright 2017 hv0912
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.vimhe.ezimp2.config.bean;

import com.vimhe.ezimp2.Constants;
import com.vimhe.ezimp2.config.exception.ConfigException;
import com.vimhe.ezimp2.util.ConvertUtils;
import com.vimhe.ezimp2.util.StringUtils;

import java.util.Collections;
import java.util.Map;

/**
 * 单元格配置对象
 *
 * @author Vimhe
 */
public final class ColumnConfig {

    /**
     * 单元格索引
     */
    private String index;

    /**
     * 对应数据库中表的列名
     */
    private String column;

    /**
     * 指定该字段的数据类型
     */
    private String type;

    /**
     * 是否读取该字段
     */
    private Boolean readable;

    /**
     * 数据验证器配置
     */
    private Map<String, ValidatorConfig> validatorConfigMap = Collections.emptyMap();

    /**
     * 数据格式转换器配置
     */
    private Map<String, FormatterConfig> formatterConfigMap= Collections.emptyMap();

    public String getIndex() {
        return index;
    }

    public void setIndex(String index) {
        if (StringUtils.isBlank(index)) {
            throw new ConfigException("必须指定配置项【cell > index】的值");
        }

        this.index = index;
    }

    public String getColumn() {
        return column;
    }

    public void setColumn(String column) {
        this.column = column;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        if (StringUtils.isBlank(type)) {
            type = Constants.CLASS_TYPE_STRING;
        }

        if (!Constants.CLASSES_TYPE.contains(type)) {
            throw new ConfigException("配置项【cell > type】的值【" + type + "】是无效的，可选值只能是下列中的一个："
                    + Constants.CLASSES_TYPE.toString());
        }

        this.type = type;
    }

    public void setReadable(Boolean readable) {
        this.readable = readable;
    }

    public Boolean getReadable() {
        return readable;
    }

    public void setReadable(String readable) {
        if (StringUtils.isBlank(readable)) {
            readable = Constants.BOOLEAN_VALUE_TRUE;
        }

        try {
            Boolean bReadable = ConvertUtils.convert(readable, Boolean.class);
            this.setReadable(bReadable);
        } catch (Exception e) {
            throw new ConfigException("配置项【cell > readable】的值【" + readable + "】是无效的，可选值只能是“true”或“false”");
        }
    }

    public Map<String, ValidatorConfig> getValidatorConfigMap() {
        return validatorConfigMap;
    }

    public void setValidatorConfigMap(Map<String, ValidatorConfig> validatorConfigMap) {
        this.validatorConfigMap = validatorConfigMap;
    }

    public Map<String, FormatterConfig> getFormatterConfigMap() {
        return formatterConfigMap;
    }

    public void setFormatterConfigMap(Map<String, FormatterConfig> formatterConfigMap) {
        this.formatterConfigMap = formatterConfigMap;
    }

}
